package com.lz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.core.domain.entity.SysUser;
import com.lz.common.utils.DateUtils;
import com.lz.common.utils.SecurityUtils;
import com.lz.common.utils.StringUtils;
import com.lz.manage.mapper.TagInfoMapper;
import com.lz.manage.model.domain.TagInfo;
import com.lz.manage.model.dto.tagInfo.TagInfoQuery;
import com.lz.manage.model.vo.tagInfo.TagInfoVo;
import com.lz.manage.service.ITagInfoService;
import com.lz.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 标签信息Service业务层处理
 *
 * @author YY
 * @date 2025-07-17
 */
@Service
public class TagInfoServiceImpl extends ServiceImpl<TagInfoMapper, TagInfo> implements ITagInfoService {
    @Resource
    private TagInfoMapper tagInfoMapper;

    @Resource
    private ISysUserService userService;

    //region mybatis代码

    /**
     * 查询标签信息
     *
     * @param tagId 标签信息主键
     * @return 标签信息
     */
    @Override
    public TagInfo selectTagInfoByTagId(Long tagId) {
        return tagInfoMapper.selectTagInfoByTagId(tagId);
    }

    /**
     * 查询标签信息列表
     *
     * @param tagInfo 标签信息
     * @return 标签信息
     */
    @Override
    public List<TagInfo> selectTagInfoList(TagInfo tagInfo) {
        List<TagInfo> tagInfos = tagInfoMapper.selectTagInfoList(tagInfo);
        for (TagInfo info : tagInfos) {
            SysUser sysUser = userService.selectUserById(info.getUserId());
            if (StringUtils.isNotNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
                info.setUserName(sysUser.getUserName());
            }
        }
        return tagInfos;
    }

    /**
     * 新增标签信息
     *
     * @param tagInfo 标签信息
     * @return 结果
     */
    @Override
    public int insertTagInfo(TagInfo tagInfo) {
        tagInfo.setUserId(+SecurityUtils.getUserId());
        tagInfo.setCreateTime(DateUtils.getNowDate());
        return tagInfoMapper.insertTagInfo(tagInfo);
    }

    /**
     * 修改标签信息
     *
     * @param tagInfo 标签信息
     * @return 结果
     */
    @Override
    public int updateTagInfo(TagInfo tagInfo) {
        tagInfo.setUpdateTime(DateUtils.getNowDate());
        return tagInfoMapper.updateTagInfo(tagInfo);
    }

    /**
     * 批量删除标签信息
     *
     * @param tagIds 需要删除的标签信息主键
     * @return 结果
     */
    @Override
    public int deleteTagInfoByTagIds(Long[] tagIds) {
        return tagInfoMapper.deleteTagInfoByTagIds(tagIds);
    }

    /**
     * 删除标签信息信息
     *
     * @param tagId 标签信息主键
     * @return 结果
     */
    @Override
    public int deleteTagInfoByTagId(Long tagId) {
        return tagInfoMapper.deleteTagInfoByTagId(tagId);
    }

    //endregion
    @Override
    public QueryWrapper<TagInfo> getQueryWrapper(TagInfoQuery tagInfoQuery) {
        QueryWrapper<TagInfo> queryWrapper = new QueryWrapper<>();
        //如果不使用params可以删除
        Map<String, Object> params = tagInfoQuery.getParams();
        if (StringUtils.isNull(params)) {
            params = new HashMap<>();
        }
        Long tagId = tagInfoQuery.getTagId();
        queryWrapper.eq(StringUtils.isNotNull(tagId), "tag_id", tagId);

        String tagName = tagInfoQuery.getTagName();
        queryWrapper.like(StringUtils.isNotEmpty(tagName), "tag_name", tagName);

        Date createTime = tagInfoQuery.getCreateTime();
        queryWrapper.between(StringUtils.isNotNull(params.get("beginCreateTime")) && StringUtils.isNotNull(params.get("endCreateTime")), "create_time", params.get("beginCreateTime"), params.get("endCreateTime"));

        Date updateTime = tagInfoQuery.getUpdateTime();
        queryWrapper.between(StringUtils.isNotNull(params.get("beginUpdateTime")) && StringUtils.isNotNull(params.get("endUpdateTime")), "update_time", params.get("beginUpdateTime"), params.get("endUpdateTime"));

        return queryWrapper;
    }

    @Override
    public List<TagInfoVo> convertVoList(List<TagInfo> tagInfoList) {
        if (StringUtils.isEmpty(tagInfoList)) {
            return Collections.emptyList();
        }
        return tagInfoList.stream().map(TagInfoVo::objToVo).collect(Collectors.toList());
    }

}
